package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.dto.ExamAssignmentCreateDTO;
import edu.sdjzu.exam.dto.ExamAssignmentDTO;
import edu.sdjzu.exam.dto.ExamAssignmentUpdateDTO;
import edu.sdjzu.exam.service.exam.ExamAssignmentService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/exam-assignments")
@RequiredArgsConstructor
@SaCheckLogin
public class ExamAssignmentController {

    private final ExamAssignmentService examAssignmentService;

    @PostMapping("/create")
    @SaCheckRole("teacher")
    public CommonResponse<Long> createExamAssignment(@Valid @RequestBody ExamAssignmentCreateDTO createDTO) {
        try {
            Long assignmentId = examAssignmentService.createExamAssignment(createDTO);
            return CommonResponse.success("考试安排创建成功", assignmentId);
        } catch (Exception e) {
            return CommonResponse.error("创建失败：" + e.getMessage());
        }
    }

    @PostMapping("/get")
    @SaCheckRole("teacher")
    public CommonResponse<ExamAssignmentDTO> getExamAssignment(@Valid @RequestBody AssignmentIdDTO assignmentIdDTO) {
        try {
            ExamAssignmentDTO assignmentDTO = examAssignmentService.getExamAssignment(assignmentIdDTO.getAssignmentId());
            return CommonResponse.success(assignmentDTO);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @PostMapping("/update")
    @SaCheckRole("teacher")
    public CommonResponse<String> updateExamAssignment(@Valid @RequestBody ExamAssignmentUpdateDTO updateDTO) {
        try {
            examAssignmentService.updateExamAssignment(updateDTO);
            return CommonResponse.success("考试安排更新成功");
        } catch (Exception e) {
            return CommonResponse.error("更新失败：" + e.getMessage());
        }
    }

    @PostMapping("/delete")
    @SaCheckRole("teacher")
    public CommonResponse<String> deleteExamAssignment(@Valid @RequestBody AssignmentIdDTO assignmentIdDTO) {
        try {
            examAssignmentService.deleteExamAssignment(assignmentIdDTO.getAssignmentId());
            return CommonResponse.success("考试安排删除成功");
        } catch (Exception e) {
            return CommonResponse.error("删除失败：" + e.getMessage());
        }
    }

    @PostMapping("/my")
    @SaCheckRole(value = {"teacher", "student"}, mode = SaMode.OR)
    public CommonResponse<List<ExamAssignmentDTO>> listMyExamAssignments() {
        try {
            List<ExamAssignmentDTO> assignments = examAssignmentService.listMyExamAssignments();
            return CommonResponse.success(assignments);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @Data
    public static class AssignmentIdDTO {
        @NotNull(message = "考试安排ID不能为空")
        private Long assignmentId;
    }
}
